<?php
	require_once $_SERVER['DOCUMENT_ROOT'] . '/SAGER/utilities/fpdf/fpdf.php';
	
	/**
	client
	*/
	$ciRif = $_POST['ciRif'];
	$clientName = $_POST['clientName'];
	$secondName = $_POST['secondName'];
	$firstLastName = $_POST['firstLastName'];
	$secondLastName = $_POST['secondLastName'];
	$emailAddress = $_POST['emailAddress'];
	
	/**
	address
	*/
	$address = $_POST['address'];
	$addressExplode = explode(", ", $address);
	$address = $addressExplode[0] . ', ' . $addressExplode[1];
	$deliveryPlace = $addressExplode[1];
	$requisitionDateMembers = explode('-', $_POST['orderDate']);
	$orderDate = $requisitionDateMembers[2] . '/' . $requisitionDateMembers[1] . '/' . 
		$requisitionDateMembers[0];
		
	/**
	  legal
	*/	
	$requisitionTax = $_POST['tax'];
	$requisitionNumber = $_POST['number'];
	$requisitionNumber = $_POST['controlNumber'];
	$products = $_POST['products'];
	
	function getProductsDetails($products) {
		$productsDetails = explode("\n", $products);
		for ($i = 0; $i < count($productsDetails); $i++) {
			$productDetails = explode(", ", $productsDetails[$i]);
			$requisitionProducts['line'][$i] = $i + 1;
			$requisitionProducts['description'][$i] = $productDetails[1];
			
			$productUnits = explode(' ', $productDetails[2]);
			$requisitionProducts['units'][$i] = substr($productUnits[1], 0, 1);
			$requisitionProducts['quantity'][$i] = $productUnits[0];
			
			$productCost = explode(' ', $productDetails[3]);
			$requisitionProducts['unit_cost'][$i] = $productCost[1];
			
			$totalCost = explode(' ', $productDetails[4]);
			$requisitionProducts['total_cost'][$i] = $totalCost[1];
		}
		
		return $requisitionProducts;
	}
	
	$pdf = new FPDF('P', 'cm', 'Letter');
	$pdf -> AddPage();
	$pdf -> SetFont('Arial', 'B', 10);
	
	/**
	 * Creating office note header
	 */
	$pdf -> Ln(3);
	$pdf -> Cell(13.4);
	$pdf -> SetDrawColor(0, 0, 128);
	$pdf -> SetLineWidth(0.05);
	$pdf -> Cell(6, 0.7, 'ORDEN DE', 'LTR', 2, 'C');
	$pdf -> Cell(3, 0.7, 'INSTALACION', 'LB', 0, 'C');	
	$pdf -> Cell(3, 0.7, $requisitionNumber, 'BR', 2, 'C');
    $pdf -> Ln(3);
	
	/**
	 * Creating customer and address header
	 */
	$pdf -> SetFont('Arial', '', 7);
	$pdf -> Cell(10, 1, 'NOMBRE: ' . $clientName . ' ' . $firstLastName, 1, 0, 'L');
	$pdf -> Cell(4.7, 1, 'No RIF: ' . $ciRif, 1, 0, 'L');
	$pdf -> Cell(4.7, 1, 'FECHA: '. $orderDate, 1, 2, 'L');
	
	$pdf -> Ln(0.3);
	$pdf -> Cell(19.4, 1, utf8_decode('DOMICILIO FISCAL Y TELÉFONO: ' . $address), 1, 2, 'L');
	$pdf -> Ln(0.3);
	
	/**
	 * Creating products header
	 */
	$pdf -> Cell(1, 1, 'RENG', 1, 0, 'C');
	$pdf -> Cell(9, 1,  utf8_decode('DESCRIPCIÓN'), 1, 0, 'C');
	$pdf -> Cell(1, 1, 'UNID', 1, 0, 'C');
	$pdf -> Cell(1, 1, 'CANT', 1, 0, 'C');
	$pdf -> Cell(2.7, 1, 'PRECIO UNITARIO', 1, 0, 'C');
	$pdf -> Cell(4.7, 1, 'TOTAL Bs', 1, 1, 'C');
	
	$requisitionTotal = 0;
	$requisitionProducts = getProductsDetails($products);
	$pdf -> SetFont('Arial', '', 7);
	for ($i = 0; $i < count($requisitionProducts['line']); $i++) {
		if ($requisitionProducts['line'][$i] < 10)
			$pdf -> Cell(1, 0.5, '0' . $requisitionProducts['line'][$i], 'LR', 0, 'C');
		else
			$pdf -> Cell(1, 0.5, $requisitionProducts['line'][$i], 'LR', 0, 'C');
		$pdf -> Cell(9, 0.5, $requisitionProducts['description'][$i], 'LR', 0, 'L');
		$pdf -> Cell(1, 0.5, $requisitionProducts['units'][$i], 'LR', 0, 'C');
		$pdf -> Cell(1, 0.5, $requisitionProducts['quantity'][$i], 'LR', 0, 'C');
		$pdf -> Cell(2.7, 0.5, $requisitionProducts['unit_cost'][$i], 'LR', 0, 'R');
		$pdf -> Cell(4.7, 0.5, $requisitionProducts['total_cost'][$i], 'LR', 1, 'R');
		$requisitionTotal += $requisitionProducts['total_cost'][$i];
	}
	
	for ($i = 0; $i < 10; $i++) {
		$pdf -> Cell(1, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(9, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(1, 0.5, '', 'LR', 0, 'L');
		$pdf -> Cell(1, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(2.7, 0.5, '', 'LR', 0, 'C');
		$pdf -> Cell(4.7, 0.5, '', 'LR', 1, 'C');
	}
	
	/**
	 * Creating footer
	 */
	$pdf -> SetFont('Arial', 'B', 6);
	$pdf -> Cell(12, 0.5, utf8_decode('CONDICIONES GENERALES: ENTREGAS PARCIALES SON PERMITIDAS Y SE FACTURARÁN EN PROPORCIÓN'), 'LTR', 0, 'L');
	$pdf -> SetFont('Arial', '', 7);
	$pdf -> Cell(2.7, 0.5, 'SUB - TOTAL Bs', 1, 0, 'L');
	if (count(explode('.', $requisitionTotal)) != 2)
		$pdf -> Cell(4.7, 0.5, $requisitionTotal . '.00', 1, 1, 'R');
	else
		$pdf -> Cell(4.7, 0.5, $requisitionTotal, 1, 1, 'R');
	$pdf -> Cell(6, 0.5, 'PLAZO: 10 - 12 SEM', 'L', 0, 'L');
	$pdf -> Cell(6, 0.5, 'LUGAR DE ENTREGA: ' . $deliveryPlace, 'R', 0, 'L');
	$pdf -> Cell(2.7, 0.5, 'IVA', 1, 0, 'L');
	$pdf -> Cell(4.7, 0.5, $requisitionTotal * ($requisitionTax / 100), 1, 1, 'R');
	$pdf -> Cell(12, 0.5, 'FORMA DE PAGO: 50% ANTICIPO, 40% A LA ENTREGA DE MATERIALES, 10% AL TERMINAR', 'LR', 0, 'L');
	$pdf -> Cell(2.7, 0.5, 'TOTAL A PAGAR Bs', 1, 0, 'L');
	$pdf -> Cell(4.7, 0.5, $requisitionTotal * (1 + $requisitionTax / 100), 1, 1, 'R');
	$pdf -> Cell(12, 0.5, '', 'LBR', 0, 'L');
	$pdf -> Cell(7.4, 0.5, 'SON:', 'LR', 1, 'L');
	$pdf -> Cell(6, 0.5, '', 'LR', 0, 'L');
	$pdf -> Cell(6, 0.5, 'CONFORME CLIENTE:', 'LR', 0, 'L');
	$pdf -> Cell(7.4, 0.5, '', 'LBR', 1, 'L');
	$pdf -> Cell(6, 0.5, 'ING. EDUARDO RICCI / TLF.: 0414.124.74.28', 'LBR', 0, 'L');
	$pdf -> Cell(6, 0.5, 'FECHA:', 'LBR', 0, 'L');
	$pdf -> Cell(7.4, 0.5, 'VA SIN TACHADURAS NI ENMENDADURAS', 1, 1, 'L');
	$pdf -> Output();
?> 